package createpattern.prototype.deepcopy.trueexample2;

public class Citation implements Cloneable{
    private Student stu;

    public Student getStu() {
        return stu;
    }

    public void setStu(Student stu) {
        this.stu = stu;
    }

    public void show() {
        System.out.println(stu.getName() + "同学：在2024学年第一学期中表现优秀，被评为三好学生。");
    }

    @Override
    protected Citation clone() throws CloneNotSupportedException {
        // 先进行浅克隆
        Citation cloned = (Citation) super.clone();
        // 对引用类型的属性进行深克隆
        cloned.stu = this.stu.clone();
        return cloned;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Citation c1 = new Citation();
        Student stu1 = new Student("张三", "广东");
        c1.setStu(stu1);

        Citation c2 = c1.clone();
        Student stu2 = c2.getStu();

        System.out.println(stu1 == stu2); // false

        c1.show();
        c2.show();

        // 修改 c2 中的 Student 属性，不会影响 c1
        c2.getStu().setName("李四");
        c1.show();  // 张三同学......
        c2.show();  // 李四同学......
    }
}
